package com.puman.watchtrade.fragment.personal.handler;

import java.lang.reflect.ParameterizedType;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.puman.watchtrade.fragment.info.model.InfoData;
import com.puman.watchtrade.fragment.info.model.InfoDatas;
import com.puman.watchtrade.fragment.personal.model.BiddRecord;
import com.puman.watchtrade.fragment.personal.model.BiddRecords;
import com.puman.watchtrade.fragment.personal.model.Charge;
import com.puman.watchtrade.fragment.personal.model.RecordInfo;
import com.puman.watchtrade.fragment.personal.model.SellRecord;
import com.puman.watchtrade.fragment.personal.model.SellRecords;
import com.puman.watchtrade.fragment.personal.model.SystemMsg;
import com.puman.watchtrade.fragment.personal.model.SystemMsgs;
import com.puman.watchtrade.fragment.set.model.UserInfo;
import com.puman.watchtrade.util.Config;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.HttpClent;

public class PersonalHttpHandler {
	/*
	 * 竞拍记录
	 */
	public DataResult biddingList(String pageNumber, String flag) {
		DataResult dataResult = new DataResult();
		BiddRecords biddRecords = new BiddRecords();
		if (GlobalData.userInfo == null) {
			return dataResult;
		}
		String path = "";
		if (flag.equals("0")) {
			path = Config.baseUrlStr + Config.biddingList + "pageNumber="
					+ pageNumber + "&userNo=" + GlobalData.userInfo.getUserNo();
		}else {
			path = Config.baseUrlStr + Config.biddingList + "pageNumber="
					+ pageNumber + "&userNo=" + GlobalData.userInfo.getUserNo()
					+"&type="+flag;
		}
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					BiddRecord biddRecord = new BiddRecord();
					biddRecord.setDatetime(userObject.getString("create_date"));
					biddRecord.setFinalPrice(userObject.getString("price"));
					biddRecord.setGoodsNo(userObject.getString("goods_no"));
					if (userObject.getString("thumbnail").equals("")) {
						biddRecord.setImageUrl("");
					} else {
						biddRecord.setImageUrl(Config.imgUrl
								+ userObject.getString("thumbnail"));
					}

					biddRecord.setIsWin(userObject.getString("win"));
					biddRecord.setMyPrice(userObject.getString("myprice"));
					biddRecord.setTitle(userObject.getString("goods_name"));
					biddRecord.setCompetionStatus(userObject
							.getString("competion_status"));
					biddRecord.setPayStatus(userObject.getString("pay_status"));
					biddRecord.setDeal(userObject.getString("deal"));
					biddRecords.biddRecordList.add(biddRecord);
				}
			}
			dataResult.object = biddRecords;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}

	/*
	 * 出售记录
	 */
	public DataResult sellRecordList(String pageNumber) {
		DataResult dataResult = new DataResult();
		SellRecords sellRecords = new SellRecords();
		String path = Config.baseUrlStr + Config.sellRecordList + "pageNumber="
				+ pageNumber + "&userNo=" + GlobalData.userInfo.getUserNo();
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					SellRecord seRecord = new SellRecord();
					seRecord.setDatetime(userObject.getString("time"));
					seRecord.setGoodsNo(userObject.getString("goods_no"));
					seRecord.setId(userObject.getString("id"));
					if (userObject.getString("pic").equals("")) {
						seRecord.setImageUrl("");
					} else {
						seRecord.setImageUrl(Config.imgUrl
								+ userObject.getString("pic"));
					}
					seRecord.setPersonCount(userObject.getString("personCount"));
					seRecord.setTitle(userObject.getString("goods_name"));
					seRecord.setCompetionStatus(userObject
							.getString("competion_status"));
					sellRecords.sellRecordList.add(seRecord);
				}
			}
			dataResult.object = sellRecords;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 系统消息
	 */
	public DataResult systemMsg(String pageNumber) {
		DataResult dataResult = new DataResult();
		SystemMsgs systemMsgs = new SystemMsgs();
		String path = Config.baseUrlStr + Config.systemList 
				+ "userNo=" + GlobalData.userInfo.getUserNo()
				+"&pageNumber="+pageNumber;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					SystemMsg systemMsg = new SystemMsg();
					systemMsg.setDatetime(userObject.getString("time"));
					systemMsg.setId(userObject.getString("id"));
					systemMsg.setMsg(userObject.getString("msg"));
					systemMsg.setType(userObject.getString("type"));
					systemMsg.setUrl(Config.webUrl+userObject.getString("url"));
					systemMsgs.systemMsgList.add(systemMsg);
				}
			}
			dataResult.object = systemMsgs;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 各项记录
	 */
	public DataResult getRecord() {
		DataResult dataResult = new DataResult();
		RecordInfo recordInfo = new RecordInfo();
		String path = Config.baseUrlStr + Config.record 
				+ "userNo=" + GlobalData.userInfo.getUserNo();
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				 JSONObject userObject = result.getJSONObject("data");
				 recordInfo.bidding = userObject.getString("auctioningCount");
				 recordInfo.bidded = userObject.getString("winCount");
				 recordInfo.unbidde = userObject.getString("notWinCount");
				 recordInfo.win = userObject.getString("tradedCount");
			}
			dataResult.object = recordInfo;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 获取charge
	 */
	public DataResult getChargeData(String jsonParam) {
		DataResult dataResult = new DataResult();
		Charge  charge = new Charge();
		String path = Config.baseUrlStr + Config.charge 
				+ "jsonParams=" + URLEncoder.encode(jsonParam);
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		Log.i("curry", "resultJson:"+resultJson);
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				JSONObject jsonObject = result.getJSONObject("data");
				charge.setObject(jsonObject.getString("object"));
				dataResult.object = jsonObject;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}


}
